clear all
set more off, perm
set maxvar 120000
set varabbrev off
* ---------------------------------------------- *
global dir 	"`1'"
global Data 	$dir/Data
global Tables 	$dir/Tables
global Figures 	$dir/Figures
global Work 	$dir/Work
global Temp 	$dir/Temp
global Pseudo	$dir/Work/Temp
* ---------------------------------------------- *
* $Work/ibes_ret generated by ibes_ret.do
use if LTG<. using $Work/ibes_ret, clear

bys datem: egen Tot=total(mcap)
gen vw=LTG*(mcap/Tot)

collapse (count) obs=vw (sum) LTG=vw, by(datem)
replace LTG=. if obs==0

tsset datem
gen d1MoFLTGagg=F1.LTG-LTG
gen d1QrFLTGagg=F3.LTG-LTG
rename LTG LTGagg
keep datem *agg

gstats winsor *agg, cuts(1 99) replace
tempfile AGG
save "`AGG'", replace
* -------------------------------------------------------- * 
use $Work/msf_adjusted, clear

tsset permno datem

forvalues x=1(2)5 {
	gen L`x'ret=1
}
forvalues x=0(1)11 {
	quietly: replace L1ret=L1ret*(1+L`x'.ret) 
}
forvalues x=0(1)35 {
	quietly: replace L3ret=L3ret*(1+L`x'.ret) 
}
forvalues x=0(1)59 {
	quietly: replace L5ret=L5ret*(1+L`x'.ret) 
}
keep permno permco datem L*ret
tempfile ret
save "`ret'", replace
* ***********************************************************************************************
use permco permno statpers medest* street_share_adj1 street_share_adj3 using $Work/FE_MO.dta, clear


gen datem=mofd(statpers)
format datem %tm

tsset permno datem
gen FEMo1=ln(medest1)-ln(street_share_adj1)
gen FEQr1=ln(medest3)-ln(street_share_adj3)

gen cpMo1=ln(F1.medest3)-ln(medest4)
gen cpQr1=ln(F3.medest1)-ln(medest4)

gstats winsor FE*1 cp*1, cuts(1 99) by(datem) replace  

su FE* cp*

keep permco permno datem FEMo1 FEQr1 cpMo1 cpQr1
tempfile FE_MO
save "`FE_MO'", replace
* ***********************************************************************************************
use $Work/Firms01_June, clear
drop bemeLTM

* add book-to-market from read_compustat_qtly.do
merge 1:1 permco permno datem using $Work/bemeLTM, keepusing(permco permno datem bemeLTM)
drop if _merge==2
drop _merge
gen lnbeme=ln(bemeLTM)
gen lnsize=ln(size)
* *****************************************************
gen retMo1=1+ret_mo1

gen retQr1=1
forvalues x=1(1)3 {
replace  retQr1=retQr1*(1+ret_mo`x')
}

forvalues x=1(1)5 {
gen  retYr`x'=1
}
forvalues x=1(1)12 {
replace  retYr1=retYr1*(1+ret_mo`x')
}
forvalues x=13(1)24 {
replace  retYr2=retYr2*(1+ret_mo`x')
}
forvalues x=25(1)36 {
replace  retYr3=retYr3*(1+ret_mo`x')
}
forvalues x=37(1)48 {
replace  retYr4=retYr4*(1+ret_mo`x')
}
forvalues x=49(1)60 {
replace  retYr5=retYr5*(1+ret_mo`x')
}
scalar alfa= 0.97749304
gen cumRetMo1=ln(retMo1)
gen cumRetQr1=ln(retQr1)
gen cumRetYr1=ln(retYr1)
gen cumRetYr3=ln(retYr1)+alfa*ln(retYr2)+alfa^2*ln(retYr3) 
gen cumRetYr5=ln(retYr1)+alfa*ln(retYr2)+alfa^2*ln(retYr3)+alfa^3*ln(retYr4)+alfa^4*ln(retYr5)

tsset permno datem
gen d1MoFLTG=F1.LTG-LTG
gen d1QrFLTG=F3.LTG-LTG

keep if cumRetYr5<.
keep if LTG<.
keep if BOOK+SIZE<.
* *****************************************************
gstats winsor                 FE* L*FE* cp* STG2 L*LTG* d*LLTG* d*FLTG lnbeme, cuts(1 99) by(datem) replace 
keep permco permno datem cum* FE* L*FE* cp* STG2 L*LTG* d*LLTG* d*FLTG lnbeme L1LTGagg d*LTGagg

merge 1:1 permco permno datem using "`FE_MO'"
keep if _merge==3
drop _merge
* *****************************************************
* gen aggregate IBES variables
foreach var of varlist FE1 FE3 FE5 cp1 cp3 cp5 d*FLTG FEMo1 FEQr1 cpMo1 cpQr1 {
	bys datem: egen `var'Agg=mean(`var')
}
* ***********************************************************************************************
merge 1:1 permno datem using "`ret'"
drop if _merge==2
drop _merge
* ***********************************************************************************************
merge m:1 datem using "`AGG'"
drop if _merge==2
drop _merge cpMo1 cpQr1
tempfile tmp
save "`tmp'", replace
* ***********************************************************************************************
use permco permno statpers medest* street_share_adj1 street_share_adj3 using $Work/FE_MO.dta, clear
gen datem=mofd(statpers)
format datem %tm

tsset permno datem
gen cpMo1=ln(F1.medest1)-ln(medest2)
gen cpQr1=ln(F3.medest1)-ln(medest4)

gstats winsor cp*1, cuts(1 99) by(datem) replace  
keep permco permno datem cp*
* ***********************************************************************************************
merge 1:1 permco permno datem using "`tmp'"

* Firm-level variables:
local ibMo1 "FEMo1 cpMo1 d1MoFLTG"
local ibQr1 "FEQr1 cpQr1 d1QrFLTG"
local ibes1 "FE1 cp1 d1FLTG"
local ibes3 "FE3 cp3 d3FLTG"
local ibes5 "FE5 cp5 d5FLTG"

* Aggregate variables:
local agMo1 "FEMo1Agg cpMo1Agg d1MoFLTGagg"
local agQr1 "FEQr1Agg cpQr1Agg d1QrFLTGagg" 
local agg1  "FE1Agg   cp1Agg   d1FLTGAgg"
local agg3  "FE3Agg   cp3Agg   d3FLTGAgg"
local agg5  "FE5Agg   cp5Agg   d5FLTGAgg"
* **********************
keep permco permno datem lnbeme cumRet* `ibMo1' `agMo1' `ibQr1' `agQr1' `ibes1' `agg1' `ibes3' `agg3' `ibes5' `agg5'
* **********************
keep if lnbeme<.
* **********************
foreach var of varlist lnbeme `ibMo1' `agMo1' `ibQr1' `agQr1' `ibes1' `agg1' `ibes3' `agg3' `ibes5' `agg5'  {
	quietly: egen sd=sd(`var')
	quietly: replace `var'=`var'/sd	
	drop sd
}
save $Temp/AppendixD2_June, replace
*
/*
eststo clear
eststo nothM1:  quietly: ivreg2	cumRetMo1 `ibMo1' `agMo1'								, dkraay(1)
predict cuM1 	if e(sample), residual
* ********
eststo nothQ1:  quietly: ivreg2	cumRetQr1 `ibMo1' `agMo1' `ibQr1' `agQr1'						, dkraay(3)
predict cuQ1 	if e(sample), residual
* ********
eststo noth1:   quietly: ivreg2	cumRetYr1 `ibMo1' `agMo1' `ibQr1' `agQr1' `ibes1' `agg1' 				, dkraay(12)
predict cum1 	if e(sample), residual
* ********
eststo noth3:   quietly: ivreg2	cumRetYr3 `ibMo1' `agMo1' `ibQr1' `agQr1' `ibes1' `agg1' `ibes3' `agg3'			, dkraay(36)
predict cum3 	if e(sample), residual
* ********
eststo noth5:   quietly: ivreg2	cumRetYr5 `ibMo1' `agMo1' `ibQr1' `agQr1' `ibes1' `agg1' `ibes3' `agg3' `ibes5' `agg5'	, dkraay(60)
predict cum5 	if e(sample), residual
*/
eststo clear
eststo nothM1:  quietly: ivreg2	cumRetMo1 `ibMo1' `agMo1'								 
predict cuM1 	if e(sample), residual
* ********
eststo nothQ1:  quietly: ivreg2	cumRetQr1 `ibMo1' `agMo1' `ibQr1' `agQr1'						 
predict cuQ1 	if e(sample), residual
* ********
eststo noth1:   quietly: ivreg2	cumRetYr1 `ibMo1' `agMo1' `ibQr1' `agQr1' `ibes1' `agg1' 				 
predict cum1 	if e(sample), residual
* ********
eststo noth3:   quietly: ivreg2	cumRetYr3 `ibMo1' `agMo1' `ibQr1' `agQr1' `ibes1' `agg1' `ibes3' `agg3'			 
predict cum3 	if e(sample), residual
* ********
eststo noth5:   quietly: ivreg2	cumRetYr5 `ibMo1' `agMo1' `ibQr1' `agQr1' `ibes1' `agg1' `ibes3' `agg3' `ibes5' `agg5'	 
predict cum5 	if e(sample), residual
*
local file "$Tables/AppendixD2_June.csv"
esttab noth* using `file', nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2 r2_a F) order(`ibMo1' `agMo1' `ibQr1' `agQr1' `ibes1' `agg1' `ibes3' `agg3' `ibes5' `agg5')  replace
* ******************
* univariate log regressions
local file "$Tables/AppendixD2_June.csv"
eststo clear
eststo woutM1: quietly: ivreg2	 cumRetMo1 	lnbeme if cuM1<., dkraay(1)
eststo woutQ1: quietly: ivreg2	 cumRetQr1 	lnbeme if cuQ1<., dkraay(3)
eststo wout1:  quietly: ivreg2	 cumRetYr1 	lnbeme if cum1<., dkraay(12)
eststo wout3:  quietly: ivreg2	 cumRetYr3 	lnbeme if cum3<., dkraay(36)
eststo wout5:  quietly: ivreg2	 cumRetYr5 	lnbeme if cum5<., dkraay(60)

esttab 		   , nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2 r2_a F)  
esttab using `file', nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2 r2_a F)  append
* **********************
eststo clear
eststo woutM1: quietly: ivreg2	cuM1	lnbeme, dkraay(1)
eststo woutQ1: quietly: ivreg2	cuQ1	lnbeme, dkraay(3)
eststo wout1:  quietly: ivreg2	cum1	lnbeme, dkraay(12)
eststo wout3:  quietly: ivreg2	cum3	lnbeme, dkraay(36)
eststo wout5:  quietly: ivreg2	cum5	lnbeme, dkraay(60)
*
esttab 		   , nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2 r2_a F)  
esttab wout* using `file', nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2 r2_a F)  append
* ----------------------------------------------------------------------------------------------------------------------- *
